package cn.edu.qzuie.artist.module.td.dal.mysql.activity;

import java.util.*;

import cn.edu.qzuie.artist.framework.common.pojo.PageParam;
import cn.edu.qzuie.artist.framework.common.pojo.PageResult;
import cn.edu.qzuie.artist.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.edu.qzuie.artist.framework.mybatis.core.mapper.BaseMapperX;
import cn.edu.qzuie.artist.module.td.controller.admin.work.vo.WorkMultiplePagesVO;
import cn.edu.qzuie.artist.module.td.controller.admin.work.vo.WorkPageReqVO;
import cn.edu.qzuie.artist.module.td.dal.dataobject.activity.ActivityDO;
import cn.edu.qzuie.artist.module.td.dal.dataobject.publishactivity.PublishActivityDO;
import cn.edu.qzuie.artist.module.td.dal.dataobject.work.WorkDO;
import cn.edu.qzuie.artist.module.td.dal.dataobject.workscatagory.WorksCatagoryDO;
import com.alibaba.excel.util.StringUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.edu.qzuie.artist.module.td.controller.admin.activity.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 活动 Mapper
 *
 * @author 泉信管理员
 */
@Mapper
public interface ActivityMapper extends BaseMapperX<ActivityDO> {

    default PageResult<ActivityDO> selectPage(ActivityPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ActivityDO>()
                .betweenIfPresent(ActivityDO::getEndTime, reqVO.getEndTime())
                .betweenIfPresent(ActivityDO::getStartTime, reqVO.getStartTime())
                .likeIfPresent(ActivityDO::getActivityName, reqVO.getActivityName())
                .eqIfPresent(ActivityDO::getActivityState, reqVO.getActivityState())
                .orderByDesc(ActivityDO::getId));
    }
    //批量更新状态
    @Update("update td_activity set activity_state = #{activityState} where id = #{id}")
    void updateBatchById(@Param("activityState")Integer activityState,  @Param("id")Long id);

    /**
     * 连表分页查询
     */
    default PageResult<ActivityPagesRespVO> selectJoinPages(PageParam pageParam, ActivityPageReqVO reqVO) {
        // 构建查询条件
        MPJLambdaWrapper<ActivityDO> wrapper = new MPJLambdaWrapper<>();

        // 查询 td_activity 表的所有字段
        wrapper.selectAll(ActivityDO.class)
                // 查询
                .select(PublishActivityDO::getNickName, PublishActivityDO::getId)
                // 左连接
                .leftJoin(PublishActivityDO.class, PublishActivityDO::getActivityId, ActivityDO::getId);
        // 动态拼接查询条件
        if (StringUtils.isNotBlank(reqVO.getActivityName())) {
            wrapper.like(ActivityDO::getActivityName, reqVO.getActivityName()); // 模糊匹配活动名称
        }
        if (reqVO.getActivityState() != null) {
            wrapper.eq(ActivityDO::getActivityState, reqVO.getActivityState()); // 精确匹配状态
        }
        //开始时间
        if (reqVO.getStartTime() != null) {
            wrapper.ge(ActivityDO::getStartTime, reqVO.getStartTime()); // 大于等于开始时间
        }
        //结束时间
        if (reqVO.getEndTime() != null) {
            wrapper.le(ActivityDO::getEndTime, reqVO.getEndTime()); // 小于等于结束时间
        }
        // 默认排序：按 创建时间 倒序
        wrapper.orderByDesc(ActivityDO::getCreateTime);
        // 执行查询并返回结果
        return selectJoinPage(pageParam, ActivityPagesRespVO.class, wrapper);
    }



}